. .\pos\01-AddADGroup.ps1
. .\pos\02-RemoveADGroup.ps1
. .\pos\03-GetShares.ps1
. .\pos\05-GetOSChecklist.ps1

Write-host "Place the Pre-requisite files and contents like Computers.txt, Groups.txt, Drives.txt, Services.txt in D:\Powershell" -foregroundcolor Yellow
Write-host "---------------------------------------------------------------------------------------"

[int]$xMenuChoiceA = 0
while ( $xMenuChoiceA -lt 1 -or $xMenuChoiceA -gt 13 ){
Write-host "1. Add AD group to Local Admin"
Write-host "2. Remove AD group to Local Admin"
Write-host "3. List shares on computer"
Write-host "4. Reset Drive Permissions"
Write-host "5. OS Check List"
Write-host "6. Get Uptime for computers"
Write-host "7. Get DNS Servers for computers"
Write-host "8. Disk Utilization"
Write-host "9. Start/Stop Services"
Write-host "10. File Copy"
Write-Host "11. CPU and Memory Count"
Write-Host "12. CPU and Memory Count"
Write-host "13. Quit"
[Int]$xMenuChoiceA = read-host "Please enter an option 1 to 12..." }
Switch( $xMenuChoiceA ){

###############################################################################
## Add AD group to Local Admin - W2K3 as well
###############################################################################
1{$comppath="D:\Powershell\Computers.txt"
$Groupspath="D:\Powershell\AddingADGroup.txt"
If ((Test-Path $comppath) -and (Test-Path $Groupspath)){

Add-ADgroup
}Else{
Write-Host "Computers.txt or AddingADGroup.txt does not exist" -ForegroundColor Red
}
}

###############################################################################
## Remove AD group to Local Admin - W2K3 as well
###############################################################################
2{$comppath="D:\Powershell\Computers.txt"
$Groupspath="D:\Powershell\RemovalGroups.txt"
If ((Test-Path $comppath) -and (Test-Path $Groupspath)){


Remove-ADgroup
}Else{
Write-Host "Computers.txt or Drives.txt does not exist" -ForegroundColor Red
}
}
###############################################################################
## List Shares on Computer - W2K3 as well
###############################################################################
3{$comppath="D:\Powershell\Computers.txt"
If (Test-Path $comppath)
{
	Get-Shares
	write-host "Check the output file SharesOut.csv at path D:\Powershell" -ForegroundColor Green
}Else{
Write-Host "Computers.txt does not exist" -ForegroundColor Red
}
}

#Reset Drive Permissions
4{$comppath="D:\Powershell\Computers.txt"
$Drivespath="D:\Powershell\Drives.txt"
If ((Test-Path $comppath) -and (Test-Path $Drivespath))
	{

	Function Reset-DrivePermissions
		{
			$computers = Get-Content "D:\Powershell\computers.txt"
			$var = Get-Content "D:\Powershell\drives.txt"
			If (($computers -ne $Null) -and ($var -ne $null)) {
				foreach ($computer in $computers)
				{
					foreach ($drive in $var) 
					{
						$Output = "Invoke-Command -ComputerName $computer {cmd.exe /c icacls $drive\* /T /Q /C /RESET}"
						$ErrorActionPreference = 'SilentlyContinue'
						$Real = Invoke-Expression $Output -ErrorVariable error
						If ($error -ne $Null) {
							write-host "Exception Message on $computer : $error" -foregroundcolor Red
						} else {
							Write-host "$computer : Drive Reset $Real" -foregroundcolor Green
						}
					}
				}
			} else {
				write-host "Pre-requisite is not in place" -ForegroundColor Red
			}
		}
		Reset-DrivePermissions
	}Else{
		Write-Host "Computers.txt and/or Drives.txt does not exist" -ForegroundColor Red
	}
}
###############################################################################
## OS Check List
###############################################################################
5{$comppath="D:\Powershell\OSChecklist_Computers.txt"
If (Test-Path $comppath){

Get-OSChecklist |
Export-CSV -NoTypeInformation D:\Powershell\OSChecklistOut-$(Get-Date -Format dd-MM-yyyy-HHmm).csv
write-host "Check the output file at path D:\Powershell\OSChecklistOut-$(Get-Date -Format dd-MM-yyyy-HHmm).csv" -ForegroundColor Green
}Else{
Write-Host "Computers.txt does not exist" -ForegroundColor Red
}
}

#Server Uptime - W2K3 as well
6{$comppath="D:\Powershell\Uptime_Computers.txt"
	If (Test-Path $comppath)
	{
		Function Get-Uptime 
		{
			$computers = Get-Content "D:\Powershell\Uptime_Computers.txt"
			If ($computers -ne $Null)
			{
				foreach ($computer in $computers) 
				{
					$Computerobj = "" | select ComputerName, Uptime, LastReboot
					$wmi = Get-WmiObject -ComputerName $computer -Query "SELECT LastBootUpTime FROM Win32_OperatingSystem"
					$now = Get-Date
					$boottime = $wmi.ConvertToDateTime($wmi.LastBootUpTime)
					$uptime = $now - $boottime
					$d =$uptime.days
					$h =$uptime.hours
					$m =$uptime.Minutes
					$s = $uptime.Seconds
					$Computerobj.ComputerName = $computer
					$Computerobj.Uptime = "$d Days $h Hours $m Min $s Sec"
					$Computerobj.LastReboot = $boottime
					$Computerobj	
				}
			} else {
				write-host "Pre-requisite is not in place" -ForegroundColor Red
			}
		}
		Get-Uptime | Export-Csv -NoTypeInformation D:\Powershell\UptimeOut.csv
		write-host "Check the output file at path D:\Powershell\UptimeOut.csv" -ForegroundColor Green
	}Else{
		Write-Host "Computers.txt does not exist" -ForegroundColor Red
	}
}

#Get DNS - W2K3 as well
7{$comppath="c:\Powershell\Computers.txt"
	If (Test-Path $comppath){

	Function Get-DNS 
		{
			$ComputerName = Get-Content "c:\Powershell\Computers.txt"
			If ($ComputerName -ne $Null) {
				foreach($Computer in $ComputerName) {
					Write-Verbose "Working on $Computer"
					if(Test-Connection -ComputerName $Computer -Count 1 -ea 0) {
			
						try {
							$Networks = Get-WmiObject -Class Win32_NetworkAdapterConfiguration `
										-Filter IPEnabled=TRUE `
										-ComputerName $Computer `
										-ErrorAction Stop
						} catch {
							Write-Verbose "Failed to Query $Computer. Error details: $_"
							continue
						}
						foreach($Network in $Networks) {
							$DNSServers = $Network.DNSServerSearchOrder
							$NetworkName = $Network.Description
							If(!$DNSServers) {
								$PrimaryDNSServer = "Notset"
								$SecondaryDNSServer = "Notset"
							} elseif($DNSServers.count -eq 1) {
								$PrimaryDNSServer = $DNSServers[0]
								$SecondaryDNSServer = "Notset"
							} else {
								$PrimaryDNSServer = $DNSServers[0]
								$SecondaryDNSServer = $DNSServers[1]
							}
							If($network.DHCPEnabled) {
								$IsDHCPEnabled = $true
							}
				
							$OutputObj  = New-Object -Type PSObject
							$OutputObj | Add-Member -MemberType NoteProperty -Name ComputerName -Value $Computer.ToUpper()
							$OutputObj | Add-Member -MemberType NoteProperty -Name PrimaryDNSServers -Value $PrimaryDNSServer
							$OutputObj | Add-Member -MemberType NoteProperty -Name SecondaryDNSServers -Value $SecondaryDNSServer
							$OutputObj | Add-Member -MemberType NoteProperty -Name IsDHCPEnabled -Value $IsDHCPEnabled
							$OutputObj | Add-Member -MemberType NoteProperty -Name NetworkName -Value $NetworkName
							$OutputObj
				
						}
					} else {
						Write-Verbose "$Computer not reachable"
					}
				}
			} else {
				write-host "Pre-requisite is not in place" -ForegroundColor Red
			}
		}
		Get-DNS | Export-Csv -NoTypeInformation C:\Powershell\DNSOut.csv
		write-host "Check the output file at path C:\Powershell\DNSOut.csv" -ForegroundColor Green
	}Else{
		Write-Host "Computers.txt does not exist" -ForegroundColor Red
	}
}

#Get Disk space - W2K3 as well
8{$comppath="c:\Powershell\Computers.txt"
If (Test-Path $comppath)
	{
	function Get-VolumeWin32 
		{
			$ComputerName = Get-Content "C:\Powershell\Computers2.txt"
			If ($ComputerName -ne $Null) 
			{
				$Query = "Select SystemName,Name,VolumeName,Size,DriveType,FreeSpace from Win32_LogicalDisk WHERE DriveType = '3'"
				$NameSpace = "root\cimv2"
				Write-Verbose "Retrieving Volume Info . . ."
				Write-Verbose $Query
				Write-Verbose "from NameSpace: $NameSpace `n"
 
				foreach ($computer in $ComputerName) 
				{
					try {
                        Get-WmiObject -Query $Query -Namespace $NameSpace -ComputerName $Computer -ErrorAction Stop |
                        Where-Object {$_.name -notmatch "\\\\" -and $_.DriveType -eq "3"} |
                        ForEach-Object {$RAW = $_ | Select-Object -Property SystemName,Name,VolumeName,Size,FreeSpace ; Write-Verbose $RAW ; $_ } |
                        Select-Object SystemName, Name, VolumeName,
                        @{name="CapacityGB";Expression={"{0:N2}"  -f ($_.size / 1gb)}},
                        @{name="UsedGB";Expression={"{0:N2}"  -f ($($_.size-$_.freespace) / 1gb)}},
                        @{name="FreeGB";Expression={"{0:N2}"  -f ($_.freespace / 1gb)}},
                        @{name="FreePC";Expression={"{0:N2}"  -f ($_.freespace / $_.size * 100)}} | Sort-Object SystemName,Name 
					}
					Catch {
                        Write-Warning "$Computer : $_"
					}
				}
			} else {
			write-host "Pre-requisite is not in place" -ForegroundColor Red
			}
		}
		Get-VolumeWin32 | Export-Csv -NoTypeInformation C:\Powershell\DiskOut.csv
		write-host "Check the output file at path C:\Powershell\DiskOut.csv" -ForegroundColor Green
	}Else{
		Write-Host "Computers.txt does not exist" -ForegroundColor Red
	}
}

# Complete Services (Start/Stop/ChangeLogon) - W2K3 as well
9{$comppath="c:\Powershell\Computers.txt"
$Servicepath="C:\Powershell\services.txt"
If ((Test-Path $comppath) -and (Test-Path $Servicepath) -and ($comppath -ne $Null) -and ($Servicepath -ne $Null))
{
Write-Host "[S] Start-Services," -ForegroundColor Cyan -NoNewline
Write-Host "[T] Stop-Services:" -ForegroundColor Cyan -NoNewline
Write-Host "[C] Change-LogOnAs :" -ForegroundColor Cyan -NoNewline
$ServiceStatus = Read-Host


Function Start-Services
{
	$services=Get-Content C:\Powershell\services.txt
	$computers=Get-Content C:\Powershell\computers2.txt
		foreach ($computer in $computers)
		{
			foreach ($service in $services)
			{
				$ErrorActionPreference = 'SilentlyContinue'
				$Call = Get-WmiObject win32_service -computername $computer -filter "Displayname='$Service'"
				If ($Call -eq $Null)
				{
					Write-Host "$Service service is not available on computer $computer" - -ForegroundColor Red
				} elseif ($call -Like "*RPC Server unavailable*")
				{
					Write-host "$computer server is not available or not reachable" -ForegroundColor Red
				} elseif (($call.startmode -eq 'Disabled') -OR ($call.startmode -eq 'Manual')) {
						Write-host "Startup type for service $service had been set to "$call.startmode" on computer $computer" -ForegroundColor Magenta
						$rtn = $Call.changestartmode("Automatic")
						If ($rtn.returnvalue -eq 0)
						{	
							Write-host "Startup type for service $service is now changed to Automatic on computer $computer" -ForegroundColor Green
							$srt = $Call.StartService()
							If ($srt.Returnvalue -eq 0) {
								Write-host "$service service is started on computer $computer" -ForegroundColor Green
							} else {
								Write-host " $($srt.returnvalue) was reported for service $service on computer $computer" -ForegroundColor Red
							}
						} else {
							Write-host " $($srt.returnvalue) was reported for Changing startup type for service $service on computer $computer" -ForegroundColor Red
						}
				} else {
					$srt = $Call.StartService()
					If ($srt.Returnvalue -eq 0) {
						Write-host "$service service is started on computer $computer" -ForegroundColor Green
					} elseif ($srt.Returnvalue -eq 10) {
						Write-host "$service service is already started on computer $computer" -ForegroundColor Yellow
					} else {
						Write-host " $($srt.returnvalue) was reported for service $service on computer $computer" -ForegroundColor Red
					}
				}
			}
		}
	
}


Function Stop-Services
{
	$services=Get-Content C:\Powershell\services.txt
	$computers=Get-Content C:\Powershell\computers.txt
	foreach ($computer in $computers)
	{
		foreach ($service in $services)
		{
			$Call = Get-WmiObject win32_service -computername $computer -filter "Displayname='$Service'"
			If ($Call -eq $Null){
				Write-Host "$Service service is not available on computer $computer" - -ForegroundColor Red
			} elseif ($call -Like "*RPC Server*"){
				Write-host "$computer server is not available or not reachable" -ForegroundColor Red
			} elseif ($call.startmode -eq 'Disabled'){
				Write-Host "$service service had been set to disabled, Hence no need to stop the service on $computer" -ForegroundColor Yellow
			} else {
				$srt = $Call.StopService()
				If ($srt.Returnvalue -eq 0) {
					Write-host "$service service is stopped on computer $computer" -ForegroundColor Green
					$csm = $call.changestartmode("Disabled")
					If ($csm.returnvalue -eq 0)
					{
						Write-host "Startup type for service $service is changed to Disabled on computer $computer" -ForegroundColor Green
					} else {
						Write-host " $($srt.returnvalue) was reported for Changing startup type for service $service on computer $computer" -ForegroundColor Red
					}
				} elseif ($srt.Returnvalue -eq 5) {
					Write-host "$service service is already stopped on computer $computer" -ForegroundColor Yellow
				} else {
					Write-host " $($srt.returnvalue) was reported for service $service" -ForegroundColor Red
				}
			}
		}
	}
}


Function Change-LogOnAs
{
	$Username = Read-Host "Domain\Username"
	$Pass = Read-Host "Password" -AsSecureString
	$Password = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($Pass))
	$services=Get-Content C:\Powershell\services.txt
	$computers=Get-Content C:\Powershell\computers.txt
	foreach ($computer in $computers)
	{
		foreach ($service in $services)
		{
			$Call = Get-WmiObject win32_service -computername $computer -filter "Displayname='$Service'"
			If ($Call -eq $Null){
				Write-Host "$Service service is not available on computer $computer" - -ForegroundColor Red
			} elseif ($call -Like "*RPC Server*"){
				Write-host "$computer server is not available or not reachable" -ForegroundColor Red
			} elseif (($call.startname -eq 'NT AUTHORITY\NetworkService') -OR ($call.startname -eq 'NT AUTHORITY\LocalService') -or ($call.startname -eq 'localSystem') -or ($call.startname -eq 'NT AUTHORITY\SYSTEM')) {
				Write-host "Service account was set to "$call.startname", which will be changed to "$Username"" -ForegroundColor Magenta
				#Options to change service account
				#uint32 Change(1 [in] string DisplayName,2 [in] string PathName,3 [in] uint32 ServiceType,4 [in] uint32 ErrorControl,5 [in] string StartMode,6 [in] boolean DesktopInteract,7 [in] string StartName,8 [in] string StartPassword,[in] string LoadOrderGroup,[in] string LoadOrderGroupDependencies,[in] string ServiceDependencies)
				$csa = $call.Change($null,$null,$null,$null,$null,$false,"$Username","$Password")
				If ($csa.Returnvalue -eq 0) {
					Write-host "Service account for service $service is changed to "$Username" on computer $computer" -ForegroundColor Green
				} elseif ($csa.returnvalue -eq 22) {
					Write-host "Service account "$Username" is not valid on this domain" -ForegroundColor Red
				} else {
					Write-host " $($csa.returnvalue) was reported while Changing service account for service $service on computer $computer" -ForegroundColor Red
				}
			} elseif ($call.startname -eq $Username) {
				Write-host "Already Service account for service $service is set to $Username on computer $computer" -ForegroundColor Yellow
			} else {
				$csa = $call.Change($null,$null,$null,$null,$null,$false,"$Username","$Password")
				If ($csa.Returnvalue -eq 0) {
					Write-host "Service account for service $service is changed to "$Username" on computer $computer" -ForegroundColor Green
				} elseif ($csa.returnvalue -eq 22) {
					Write-host "Service account "$Username" is not valid on this domain" -ForegroundColor Red
				} else {
					Write-host " $($csa.returnvalue) was reported while Changing service account for service $service on computer $computer" -ForegroundColor Red
				}
			}
		}
	}
}
				
If ($ServiceStatus -eq 'S') {Start-Services}
If ($ServiceStatus -eq 'T') {Stop-Services}
If ($ServiceStatus -eq 'C') {Change-LogOnAs}

}Else{
		Write-Host "Pre-requisite is not in place" -ForegroundColor Red
}
}

#FileCopy
10{$comppath="c:\Powershell\Computers.txt"
	If ((Test-Path $comppath) -and ($comppath -ne $Null))
	{
		Function File-copy
		{
			$computer = Get-Content -path C:\powershell\Computers.txt
			$Destination = Read-Host "Enter File destination (windows\temp)"
			Foreach ($comp in $computer)
			{
				$Testfolder = Test-Path \\$comp\D$\IBM\WebSphere\AppServer\profiles\Custom01\logs\POS*
				If ($Testfolder -eq 'True')
				{
					$Foldername = Get-ChildItem \\$comp\D$\IBM\WebSphere\AppServer\profiles\Custom01\logs\POS* | Select-Object -ExpandProperty Name
					$Foldername | Out-File -Append \\$env:ComputerName\c$\Powershell\Sources.txt
					$Sources = Get-Content -path \\$env:ComputerName\c$\Powershell\Sources.txt
				}
				Foreach ($source in $Sources)
				{
					$TestPath = Test-Path \\$comp\D$\IBM\WebSphere\AppServer\profiles\Custom01\logs\$source
					If ($TestPath -eq 'True')
					{
						Copy-Item \\$comp\D$\IBM\WebSphere\AppServer\profiles\Custom01\logs\$Source\SystemOut.log -Destination \\$env:ComputerName\c$\$Destination
						Rename-Item \\$env:ComputerName\c$\$Destination\SystemOut.log SystemOut_"$comp"_"$Source".log
					}
				}
			}
			Remove-Item \\$env:ComputerName\c$\Powershell\Sources.txt | out-null
			Write-host "SystemOut.log copied to C:\$Destination on $env:ComputerName for given list of computers" -Foreground Green
		}
		File-copy
	}Else{
		Write-Host "Pre-requisite is not in place" -ForegroundColor Red
	}
}


#CPU and Memory - W2K3 as well
11{$comppath="c:\Powershell\Computers.txt"
	If (Test-Path $comppath)
	{

		function Get-CPUnRAM
		{

			$computers = Get-Content -path C:\Powershell\Computers.txt
			If ($computers -ne $Null) 
			{
				foreach ($computer in $computers) 
				{
					#$adapters = Get-WmiObject -Class Win32_NetworkAdapterConfiguration -ComputerName $computer -Filter 'NetConnectionID = "Public VLAN Connection" OR NetConnectionID = "Backup VLAN Connection"'
					$adapters = Get-WmiObject -Class win32_networkadapter -ComputerName $computer
					$computerName = $adapters[0].PSComputerName
					$CPU = Get-WmiObject -computername $computer Win32_ComputerSystem | Select-Object -ExpandProperty NumberofProcessors
					$RAM = get-wmiobject -class "Win32_ComputerSystem" -namespace "root\CIMV2" -computername $computer
					foreach ($Mem in $RAM)
					{
						$Memory = [math]::round($Mem.TotalPhysicalMemory/1024/1024/1024, 0)
					}

					$properties = @{
						'ComputerName'= $computerName
						'CPU' = $CPU
						'RAM' = $Memory
					}

					$Output = New-Object -TypeName psobject -Property $properties | Select-Object ComputerName,CPU,RAM
					Write-Output $Output
				}
			} else {
				write-host "Pre-requisite is not in place" -ForegroundColor Red
			}
		}
		Get-CPUnRAM |
		Export-CSV -NoTypeInformation C:\Powershell\CPUandRAMout.csv
		write-host "Check the output file at path C:\Powershell\CPUandRAMout.csv" -ForegroundColor Green
	}Else{
		Write-Host "Computers.txt does not exist" -ForegroundColor Red
	}
}

default{<#run a default action or call a function here #>}
}

<#Disable Services
8{$comppath="c:\Powershell\Computers.txt"
$Servicepath="C:\Powershell\services.txt"
If ((Test-Path $comppath) -and (Test-Path $Servicepath))
	{
	Function Disable-Services
		{
			$services=Get-Content C:\Powershell\services.txt
			$computers=Get-Content C:\Powershell\computers.txt
			If (($Computers -ne $Null) -and ($services -ne $Null))
			{
				get-service -DisplayName $services -computer $computers |
				set-service -StartupType disabled -PassThru|
				stop-service -PassThru -ea 0
			} else {
				write-host "Pre-requisite is not in place" -ForegroundColor Red
			}
		}
		Disable-Services
	}Else{
		Write-Host "Computers.txt or Services.txt does not exist" -ForegroundColor Red
	}
}#>
<#Start Services
10{$comppath="c:\Powershell\Computers.txt"
$Servicepath="C:\Powershell\services.txt"
If ((Test-Path $comppath) -and (Test-Path $Servicepath))
	{
	Function Start-Services
		{
			$services=Get-Content C:\Powershell\services.txt
			$computers=Get-Content C:\Powershell\computers.txt
			If (($Computers -ne $Null) -and ($services -ne $Null))
			{
				get-service -DisplayName $services -computer $computers |
				set-service -StartupType Automatic -PassThru|
				start-service -PassThru -ea 0
			} else {
				write-host "Pre-requisite is not in place" -ForegroundColor Red
			}
		}
		Start-Services
	}Else{
		Write-Host "Computers.txt or Services.txt does not exist" -ForegroundColor Red
	}
}#>